package com.dm.webCrawler.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.io.FileUtil;
import com.aspose.words.*;


public class AsponseUtil {
	static {
		getLicense();
	}
	
	public static void setFontsPath(String fontPath) {
		FontSettings.getDefaultInstance().setFontsFolder(fontPath, true);
	}
	
	 public static boolean getLicense() {
	        boolean result = false;
	        try {
	        	/*String licensePath=asponseUtil.class.getResource("license.xml").getFile();
	        	System.out.println(licensePath);*/
//	        	FileInputStream is=new FileInputStream(licensePath);
//	        	ClassLoader loader = Thread.currentThread().getContextClassLoader();
//				FileInputStream is = new FileInputStream(loader.getResource("license.xml").getPath());// 凭证文件
	        	
	        	FileInputStream is = new FileInputStream("license.xml");
	            License aposeLic = new License();
	            aposeLic.setLicense(is);
	            result = true;
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return result;
	    }
	
	
	 /**
	  * word转html
	  * @param wordPath 待转换的word文档
	  * @param savePath 保存的文件路径
	  * @param relativeImgDir 图片文件相对存储路径
	  * @return
	 * @throws Exception 
	  */
	 public static String word2Html(String wordPath,String savePath,String relativeImgDir) throws Exception {
		 wordPath=wordPath.replace("\\", "/");
		 savePath=savePath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx") && !wordPath.endsWith(".wps")) {
			 throw new Exception("待转换的word文件格式错误！"+wordPath);
		 }
		 
		 if(!savePath.endsWith(".htm") && !savePath.endsWith(".html")) {
			 throw new Exception("转换文件格式错误！"+savePath);
		 }
		 
		 File saveFile=new File(savePath);
		 if(!saveFile.exists()) {
			 File pFile = saveFile.getParentFile();
			 if(!pFile.exists()) {
				 pFile.mkdirs();
			 }
			 /*saveParent.createNewFile();*/
		 }
		 FileInputStream in = new FileInputStream(new File(wordPath).getAbsolutePath());
		Document doc = new Document(in); // word文档
		 
		 HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
		 options.setEncoding(Charset.forName("UTF-8"));
		 options.setExportFontsAsBase64(true);
		    options.setExportImagesAsBase64(true);
		    options.setPrettyFormat(true);
			options.setExportTextInputFormFieldAsText(true);
			if(relativeImgDir==null || relativeImgDir.trim().equals("")){
				relativeImgDir="";
			}else{
				String filePath = saveFile.getParentFile().getAbsolutePath();
				filePath=filePath.replace("\\", "/");
				filePath=filePath.endsWith("/")?filePath:filePath+"/";
				options.setImagesFolder(filePath+relativeImgDir);
				options.setImagesFolderAlias(relativeImgDir);
			}
			
			
		 
		 SaveOutputParameters saveOut = doc.save(savePath, options);
		 in.close();
		 return saveOut.getContentType();
		 
	 }
	 
	 public static String word2Html(String wordPath,String savePath) throws Exception {
		 wordPath=wordPath.replace("\\", "/");
		 savePath=savePath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx") && !wordPath.endsWith(".wps")) {
			 throw new Exception("待转换的word文件格式错误！"+wordPath);
		 }
		 
		 if(!savePath.endsWith(".htm") && !savePath.endsWith(".html")) {
			 throw new Exception("转换文件格式错误！"+savePath);
		 }
		 
		 File saveFile=new File(savePath);
		 if(!saveFile.exists()) {
			 File pFile = saveFile.getParentFile();
			 if(!pFile.exists()) {
				 pFile.mkdirs();
			 }
			 /*saveParent.createNewFile();*/
		 }
//		 FileInputStream in = new FileInputStream(new File(wordPath).getAbsolutePath());
		Document doc = new Document(wordPath); // word文档
		 
		 HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.HTML);
		 options.setEncoding(Charset.forName("UTF-8"));
		 options.setExportFontsAsBase64(true);
		    options.setExportImagesAsBase64(true);
		    options.setPrettyFormat(true);
			options.setExportTextInputFormFieldAsText(true);
//			if(relativeImgDir==null || relativeImgDir.trim().equals("")){
//				relativeImgDir="";
//			}else{
//				String filePath = saveFile.getParentFile().getAbsolutePath();
//				filePath=filePath.replace("\\", "/");
//				filePath=filePath.endsWith("/")?filePath:filePath+"/";
//				options.setImagesFolder(filePath+relativeImgDir);
//				options.setImagesFolderAlias(relativeImgDir);
//			}
		 SaveOutputParameters saveOut = doc.save(savePath, options);
//		 in.close();
		 return saveOut.getContentType();
		 
	 }
	 
	 /**
	  * word转pdf
	  * @param wordPath
	  * @param pdfPath
	 * @throws Exception 
	  */
	public static void word2Pdf(String wordPath, String pdfPath) throws Exception {
		
		wordPath=wordPath.replace("\\", "/");
		pdfPath=pdfPath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx")) {
			 throw new Exception("待转换的word文件格式错误！"+wordPath);
		 }
		 
		 if(!pdfPath.endsWith(".pdf") && !pdfPath.endsWith(".pdf")) {
			 throw new Exception("转换文件格式错误！"+pdfPath);
		 }
//		 FileOutputStream os=null;
		try {
//			File file = new File(pdfPath);
//			os = new FileOutputStream(file);
			Document doc = new Document(wordPath); // word文档
			// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
			doc.save(pdfPath, SaveFormat.PDF);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
//			try {
//				os.close();
//			} catch (Exception e2) {
//				// TODO: handle exception
//			}
		}

	}
	
	/**
	 * word文本比对  word2比较word1
	 * @param word1 基准文档存储路径
	 * @param word2 比较文档 存储路径
	 * @param exportWord 导出的比较结果文档存储路径
	 * @throws Exception
	 */
	public static void compare(String word1,String word2,String exportWord) throws Exception {
		
		Document doc1 = new Document(word1);
		Document doc2 = new Document(word2);
		CompareOptions compareOptions = new CompareOptions();
		compareOptions.setIgnoreFormatting(true);//是否忽略格式
		compareOptions.setTarget(ComparisonTargetType.NEW);//以新文档作为差异输出文件
//		compareOptions.setTarget(ComparisonTargetType.CURRENT);//以当前文档作文差异输出文件
//		compareOptions.
		doc1.compare(doc2, "test", new Date(), compareOptions);

		/**
		 * compare
		 * 将此文档与另一个文档进行比较，该文档根据编辑和格式修订的数量进行更改 调整。
			目前不比较以下文档节点：
			StructuredDocumentTag
			项目3
			注意： 文档在比较之前不得进行修订。
			参数：
			document - 要比较的文件。
			author - 作者用于修订的缩写。
			dateTime - 用于修订的日期和时间。
			options 允许使用指定比较选项CompareOptions。
		 */
		
		doc1.save(exportWord);
	}
	
	/**
	 * 使用builder分方式将html文档转换为word
	 * @param htmlPath
	 * @param wordPath
	 * @throws Exception
	 */
	public static void html2wordAsBuilder(String htmlPath,String wordPath) throws Exception{
		
		wordPath=wordPath.replace("\\", "/");
		 htmlPath=htmlPath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx")) {
			 throw new Exception("导出文档格式错误！！"+wordPath);
		 }
		 
		 if(!htmlPath.endsWith(".htm") && !htmlPath.endsWith(".html")) {
			 throw new Exception("读取文档格式错误！"+htmlPath);
		 }
		int saveFormat=SaveFormat.DOC;
		if(wordPath.endsWith(".docx")) {
			saveFormat=SaveFormat.DOCX;
		}
		String fileValues= FileUtil.readUtf8String(htmlPath);
		File sourceFileF=new File(htmlPath);
		sourceFileF=new File(sourceFileF.getAbsolutePath());//文件不一定是绝对路径，所有要先抽取绝对路径才能获取父文件夹绝对路径
		String fileParentPath=sourceFileF.getParentFile().getAbsolutePath();
		fileParentPath=fileParentPath.replace("\\", "/");
		if(!fileParentPath.endsWith("/")){
			fileParentPath+="/";
		}

		Pattern p=Pattern.compile("src=\"(.*?)\"");
		Matcher matcher = p.matcher(fileValues);
		StringBuffer sb=new StringBuffer();
		while (matcher.find()) {
//			String g0=matcher.group(0);
			String g1=matcher.group(1);
			
			if(!g1.matches("([A-Z]:/|[a-z]:/|http://|https:/|/).*?")){
				matcher.appendReplacement(sb, "src=\""+fileParentPath+g1+"\"");
//				System.out.println("未匹配到");
			}else{
//				System.out.println("匹配到");
			}
		}
		
		String newValueStr = matcher.appendTail(sb).toString();
		Document doc = new Document();
		DocumentBuilder builder=new DocumentBuilder(doc);
		builder.insertHtml(newValueStr);
        doc.save(wordPath, saveFormat);//生成doc文件
	}
	
	
	/**
	 * html转word
	 * @param htmlPath html路径
	 * @param wordPath 保存的word路径
	 * @throws Exception
	 */
	public static void html2word(String htmlPath,String wordPath) throws Exception{
		 wordPath=wordPath.replace("\\", "/");
		 htmlPath=htmlPath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx")) {
			 throw new Exception("导出文档格式错误！！"+wordPath);
		 }
		 
		 if(!htmlPath.endsWith(".htm") && !htmlPath.endsWith(".html")) {
			 throw new Exception("读取文档格式错误！"+htmlPath);
		 }
		int saveFormat=SaveFormat.DOC;
		if(wordPath.endsWith(".docx")) {
			saveFormat=SaveFormat.DOCX;
		}
		HtmlLoadOptions lo = new HtmlLoadOptions();
//		lo.setEncoding(Charset.forName("UTF-8"));
		//HtmlControlType 包含常量的实用程序类。表示从HTML导入的<input>和<select>元素的文档节点类型。
        lo.setPreferredControlType(HtmlControlType.STRUCTURED_DOCUMENT_TAG);
        //Load the HTML document
        Document doc = new Document(htmlPath, lo);
//		FontSettings setting = new FontSettings();
//		setting.setDefaultFontName("仿宋");
//		doc.setFontSettings(setting);
        //Save the HTML document as DOCX
        doc.save(wordPath, saveFormat);
	}
	
	/**
	 * word书签替换
	 * @param wordPath word路径
	 * @param replaceData 替换键值对   书签名称->书签值
	 * @throws Exception 
	 */
	public static void replaceBookMark(String wordPath,Map<String, String> replaceData) throws Exception {
		
		 wordPath=wordPath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx") && !wordPath.endsWith(".wps")) {
			 throw new Exception("替换书签的文件格式错误！"+wordPath);
		 }
		 if(replaceData==null || replaceData.isEmpty()) {
			 return;
		 }
		 Document doc = new Document(wordPath);
		 BookmarkCollection bookMarks = doc.getRange().getBookmarks();
		 if(bookMarks==null || bookMarks.getCount()<=0) {
			 return ;
		 }
		 
		 /*bookMarks.forEach(new Consumer<Bookmark>() {

			@Override
			public void accept(Bookmark t) {

				try {
					System.out.println(t.getName() +"         "+t.getText());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});*/
		 
		 replaceData.forEach(new BiConsumer<String, String>() {
			@Override
			public void accept(String bookMarkName, String value) {
				Bookmark bookMark = bookMarks.get(bookMarkName);
				if(bookMark!=null) {
					try {
						bookMark.setText(value);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		 doc.save(wordPath,SaveFormat.DOC);
		
	}
	
	/**
	 * word内容替换
	 * @param wordPath word文件路径
	 * @param replaceData 替换对照键值对
	 * @param matchCase 是否区分大小写
	 * @throws Exception 
	 */
	public static void replaceValue(String wordPath,Map<String, String> replaceData,boolean matchCase) throws Exception {
		 wordPath=wordPath.replace("\\", "/");
		 if(!wordPath.endsWith(".doc") && !wordPath.endsWith(".docx")) {
			 throw new Exception("替换书签的文件格式错误！"+wordPath);
		 }
		 if(replaceData==null || replaceData.isEmpty()) {
			 return;
		 }
		 Document doc = new Document(wordPath);
		 
		 FindReplaceOptions options = new FindReplaceOptions();//替换规则
		 options.setMatchCase(matchCase);//是否区分大小写
		 options.setFindWholeWordsOnly(false);//按字匹配
		 
		 replaceData.forEach(new BiConsumer<String, String>() {
			@Override
			public void accept(String oldVal, String newVal) {
				try {
					doc.getRange().replace(oldVal, newVal, options);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		 });
		 
		 doc.save(wordPath);
		
	}
	
	/**
	 * 根据标题样式拆分word，如果没找到将返回原数据
	 * @param sourceDoc 源文件
	 * @param outDir 输出目录
	 * @param type 样式类型 标题 1~9 ,大于当前标题的节点也自动拆分
	 * @return 
	 * @throws Exception 
	 */
	public static boolean splitDocByTitle(String sourceDoc,String outDir,int type) throws Exception{
		sourceDoc=sourceDoc.replace("\\", "/");
		outDir=outDir.replace("\\", "/");
		
		if(type<1 || type>9) {
			throw new Exception("按标题拆分word文档标题级别不在可支持的范围");
		}
		int splitStyle=StyleIdentifier.HEADING_1;
		switch (type) {
		case 1:
			splitStyle=StyleIdentifier.HEADING_1;
			break;
		case 2:
			splitStyle=StyleIdentifier.HEADING_2;
			break;
		case 3:
			splitStyle=StyleIdentifier.HEADING_3;
			break;
		case 4:
			splitStyle=StyleIdentifier.HEADING_4;
			break;
		case 5:
			splitStyle=StyleIdentifier.HEADING_5;
			break;
		case 6:
			splitStyle=StyleIdentifier.HEADING_6;
			break;
		case 7:
			splitStyle=StyleIdentifier.HEADING_7;
			break;
		case 8:
			splitStyle=StyleIdentifier.HEADING_8;
			break;
		case 9:
			splitStyle=StyleIdentifier.HEADING_9;
			break;
	
		default:
			return false;
		}
		
		 Document mDoc = new Document(sourceDoc);
		 NodeCollection<Paragraph> paras = mDoc.getChildNodes(NodeType.PARAGRAPH, true);//取所有段落
	        DocumentBuilder builder = new DocumentBuilder(mDoc);
	        for (Paragraph para :  paras)
	        {
	            int style = para.getParagraphFormat().getStyleIdentifier();
	            if (style>0 && style<10 &&style <= splitStyle) {
	            	//判断段落是标题类型
	                Section section = para.getParentSection();//获取父层的节点
		            
		            if (para != section.getBody().getFirstParagraph())
		            {
		            	//如果当前段落不是所在节的第一章，那么将段落前加上分节符并从新页面开始
		                builder.moveTo(para.getFirstChild());
		                builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);//添加节符号
		                section.getBody().getLastParagraph().remove();//为什么要删除后面的东西没搞懂
		            }
	            } 
	        }
	        SectionCollection sections = mDoc.getSections();
	        int x=1;
	        for (Section section : sections)//遍历筛选出的段落集合
	        {
	            
	            
	            Document dummyDoc = new Document();
                dummyDoc.removeAllChildren();//清理所有节点
                /**
                 * 获取导入节点，导入子节点、按原样式导入
                 */
                Node importNode = dummyDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
                dummyDoc.appendChild(importNode);//

                //设置文档头名称
                String paraText = section.getBody().getFirstParagraph().getText();
                dummyDoc.getBuiltInDocumentProperties().setTitle(paraText);

//                HtmlSaveOptions saveOptions = new HtmlSaveOptions();
//                saveOptions.setPrettyFormat(true);
//                saveOptions.setAllowNegativeIndent(true);//设置代码内容不压缩
//                saveOptions.setExportHeadersFootersMode(ExportHeadersFootersMode.NONE);//指定页眉和页脚输出方式不输出
//                saveOptions.setImagesFolder("doc/");
//                dummyDoc.save("doc/split/out/TableView_"+x+".doc", saveOptions);
                File dir=new File(outDir);
                if(!dir.exists()) {
                	dir.mkdirs();
                }
                String fileName=new File(sourceDoc).getName();
                String singeName=fileName.substring(0, fileName.indexOf("."));
                dummyDoc.save((!outDir.endsWith("/")?outDir+"/":outDir)+singeName+x+".doc", SaveFormat.DOC);
                x++;
	            
	        }
		return true;
	}
	
	/**
	 * 按数量拆分，将文档平均拆分成多分,内部按段落数量平均分
	 * @param sourceDoc 源文件
	 * @param outDir 拆分到目录
	 * @param size 拆分数量 
	 * @throws Exception Exception
	 */
	public static void splitBySize(String sourceDoc,String outDir,int size) throws Exception {
		if(size<2) {
			throw new Exception("文档拆分数量需大于1");
		}
		if(size>100) {
			throw new Exception("文档拆分数量不能大于30");
		}
		sourceDoc=sourceDoc.replace("\\", "/");
		outDir=outDir.replace("\\", "/");
		Document mDoc = new Document(sourceDoc);
		SectionCollection zSections = mDoc.getSections();
		Section firstSection=null;
		//清理掉多余的分节符
		for (Section section : zSections) {
			if(firstSection==null) {
				firstSection=section;
			}else {
				firstSection.appendContent(section);
				section.remove();
			}
		
		}
		 NodeCollection<Paragraph> paras = mDoc.getChildNodes(NodeType.PARAGRAPH, true);//取所有段落
		 if(paras.getCount()<size) {
			 throw new Exception("段落数量不足，无法拆分");
		 }
		 
		 /*
		  * 
		 //按文本长度拆分存在暂时不可调和的bug
		 String text = mDoc.getText();
		 Integer textSize=text.length();
		 if(textSize<100) {
			 throw new Exception("文本长度需大于100才能执行拆分");
		 }*/
		 int singeParaSize = paras.getCount()/size; //平均每份段落数量
		 int remainder=paras.getCount()%size;//余数
        DocumentBuilder builder = new DocumentBuilder(mDoc);
        int curParaConnt=1;//计数
        int curSize=0;
        for (Paragraph para :  paras)
        {
        	
//        	System.out.println(para.getText()+"   "+para.getNodeType());
        	
        	/**
        	 * 开始向文档中插入 节
        	 */
        	if((curParaConnt==singeParaSize && remainder==0) || curParaConnt>singeParaSize) {
        		curSize++;
        		if(curSize>=size) {
        			continue;
        		}
        		 Section section = para.getParentSection();//获取父层的节点
 	            if (para != section.getBody().getFirstParagraph())
 	            {
 	            	//如果当前段落不是所在节的第一章，那么将段落前加上分节符并从新页面开始
 	            	
 	            	if(para.getFirstChild()==null) {
 	            		builder.moveTo(para);
 	            	}else {
 	            		builder.moveTo(para.getFirstChild());
 	            	}
 	                
 	                builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);//添加节符号
 	                section.getBody().getLastParagraph().remove();//为什么要删除后面的东西没搞懂
 	            }
 	           curParaConnt=0;
 	           continue;
        	}
        	
        	if(curParaConnt==singeParaSize && remainder>0) {
        		remainder--;
        	}
        	
        	curParaConnt++;

        }
        SectionCollection sections = mDoc.getSections();
        int x=1;
        for (Section section : sections)//遍历筛选出的段落集合
        {
            Document dummyDoc = new Document();
            dummyDoc.removeAllChildren();//清理所有节点
            /**
             * 获取导入节点，导入子节点、按原样式导入
             */
            Node importNode = dummyDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            dummyDoc.appendChild(importNode);//

            //设置文档头名称
            String paraText = section.getBody().getFirstParagraph().getText();
            dummyDoc.getBuiltInDocumentProperties().setTitle(paraText);

            
            File dir=new File(outDir);
            if(!dir.exists()) {
            	dir.mkdirs();
            }
            String fileName=new File(sourceDoc).getName();
            String singeName=fileName.substring(0, fileName.indexOf("."));
            dummyDoc.save((!outDir.endsWith("/")?outDir+"/":outDir)+singeName+x+".doc", SaveFormat.DOC);
            x++;
            
        }
		
	}
	
	
	public static void mergeFiles(List<String> files,String outFile) throws Exception {
		
		if(files==null || files.size()<2) {
			throw new Exception("合并文件请至少保持两个");
		}
		int exportFormate=0;
		if(outFile.toLowerCase().endsWith(".doc") ) {
			exportFormate=SaveFormat.DOC;
		}else if(outFile.toLowerCase().endsWith(".docx")) {
			exportFormate=SaveFormat.DOCX;
		}else {
			throw new Exception("输出文件格式错误！");
		}
		
		
		Document mDoc = null;
		for (String file : files) {
			Document zDoc=null;;
			LoadOptions load=new LoadOptions();
			if(file.toLowerCase().endsWith(".txt")) {
				load.setLoadFormat(LoadFormat.TEXT);
				zDoc = new Document(file,load);
			}else if(file.toLowerCase().endsWith(".doc")) {
				load.setLoadFormat(LoadFormat.DOC);
				zDoc = new Document(file,load);
			}else if(file.toLowerCase().endsWith(".docx")) {
				load.setLoadFormat(LoadFormat.DOCX);
				zDoc = new Document(file,load);
			}else if(file.toLowerCase().endsWith(".html") || file.toLowerCase().endsWith(".htm")) {
				load.setLoadFormat(LoadFormat.HTML);
				zDoc = new Document(file,load);
			}else {
				continue;
			}
			if(mDoc==null) {
				mDoc=zDoc;
			}else {
				mDoc.appendDocument(zDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
			}
			
		}
		
		File outF=new File(outFile);
		if(!outF.exists()) {
			if(!outF.getParentFile().exists()) {
				outF.mkdirs();
			}
			outF.createNewFile();
		}
		
		mDoc.save(outFile, exportFormate);
		
	}
	
	/**
	 * asponse 读取批注信息
	 * @param file 文件
	 * @throws Exception
	 */
	public static void getCommentInfo(String file) throws Exception{
		LoadOptions load=new LoadOptions();
		load.setLoadFormat(LoadFormat.DOC);
		Document doc=new Document(file,load);
		NodeCollection<Paragraph> paras = doc.getChildNodes(NodeType.PARAGRAPH, true);//取所有段落
		SectionCollection sections = doc.getSections();
		System.out.println("总页数:"+doc.getPageCount());
		LayoutCollector layoutCollector = new LayoutCollector(doc);
	    doc.updatePageLayout();
	    int x=0;
	    for (Paragraph para : paras) {
	    	NodeCollection comments = para.getChildNodes(NodeType.COMMENT, true);
			if(comments.getCount()>0){
				for (int index=0;index<comments.getCount();index++) {
					Comment comment = (Comment)comments.get(index);
//					CompositeNode parent = comment.getParentNode();
//					System.out.println(comment.getParentNode().getText());
					Node  previousSiblingNode= comment.getPreviousSibling().getPreviousSibling();
					List<String> data=new ArrayList<>();
					while(previousSiblingNode.getNodeType()!=NodeType.COMMENT_RANGE_START) {
						data.add(previousSiblingNode.getText());
						previousSiblingNode=previousSiblingNode.getPreviousSibling();
					}
					Collections.reverse(data);
					System.out.println("被批注的内容:"+String.join(",", data)+" 批注内容:"+comment.getText()+" 批注时间:"+comment.getDateTime()+"  所属页:"+layoutCollector.getStartPageIndex(comment)+" 行:"+x);
				}
			}
			x++;
		}
	}


	public static void testReadLines(String filePath) throws Exception {

		LoadOptions load=new LoadOptions();
		load.setLoadFormat(LoadFormat.DOC);
		Document doc=new Document(filePath,load);
		NodeCollection<Paragraph> paras = doc.getChildNodes(NodeType.PARAGRAPH, true);//取所有段落
		SectionCollection sections = doc.getSections();
		System.out.println("总页数:"+doc.getPageCount());
		LayoutCollector layoutCollector = new LayoutCollector(doc);
		doc.updatePageLayout();
		int x=0;
		for (Paragraph para : paras) {
			System.out.println(para.getText());
			Node[] childs = para.getChildNodes().toArray();
			for (Node child : childs) {
				System.out.println(child.getNodeType());
			}
			x++;
		}
	}
	
	public static void main(String[] args) throws Exception {
//		word2Html("doc/pdf/CentOS7.docx","doc/pdf/CentOS7_2.html");
		//testReadLines("doc/红头文件模板test.doc");
//		word2Pdf("doc/香港的新闻.doc", "doc/香港的新闻.pdf");
		
//		OsInfo info = new OsInfo();
//    	if(info.isLinux()) {
//    		asponseUtil.setFontsPath("字库目录地址");
//    	}
//    	asponseUtil.word2Pdf("word地址","pdf地址");
		html2word("C:\\Users\\Administrator\\Desktop\\528008\\528008.html","C:\\Users\\Administrator\\Desktop\\528008\\528008.doc");
	}
	
}
